<?php
/**
 * Created by PhpStorm.
 * User:tsingStar
 * Date: 2019/7/30
 * Time: 9:46
 */

namespace app\admin\controller;


use app\common\model\Storage;

class Chat extends Common
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $this->templateTitle("");
        $this->view->engine->layout(false);
        //获取最近三小时聊天记录
        $user_id = input("user_id");
        $list = db("message")->where("send_id=".$user_id." or receive_id=".$user_id)->where("create_time", ">", time()-30*24*60*60)->order("create_time asc")->select();
        $this->assign("list", $list);
        $user = \app\common\model\User::exists($user_id);
        $this->assign("avatar", $user["avatar"]);
        $this->assign("logo", "/static/home/images/avatar.png");
        //最后一条聊天记录
        $last_id = db("message")->where("send_id=".$user_id." or receive_id=".$user_id)->order("create_time desc")->value("id");
        $this->assign("last_id", empty($last_id)?0:$last_id);
        $this->assign("user_id", $user_id);
        return $this->fetch("index");
    }

    /**
     * 获取聊天状态
     */
    public function getStatus()
    {
        //获取最近一小时聊天记录
        $list = db("message")->where("create_time", ">", time()-30*24*60*60)->order("id asc")->select();
        $data = [];
        foreach ($list as $item) {
            $uid = $item["send_id"] != 0 ?$item["send_id"]: $item["receive_id"];
            if (isset($data[$uid])) {
                $data[$uid]["last_id"] = $item["id"];
                $data[$uid]["origin_id"] = $item["id"];
            } else {
                $user = \app\common\model\User::exists($uid);
                $data[$uid] = [
                    "user_id" => $uid,
                    "nick_name" => $user["nick_name"],
                    "status" => 0,
                    "last_id" => $item["id"],
                    "origin_id"=>$item["id"]
                ];
            }
        }
        $msg_list = input("data/a");
        $status = 0;
        foreach ($msg_list as $item) {
            $l_id = db("message")->where("send_id|receive_id",$item["user_id"])->where("id", ">", $item["last_id"])->order("id desc")->value("id");
            if(!empty($l_id)){
                if(isset($data[$item["user_id"]])){
                    unset($data[$item["user_id"]]);
                    $data = array_merge([$item["user_id"]=>[
                        "user_id"=>$item["user_id"],
                        "nick_name"=>$item["nick_name"],
                        "status"=>1,
                        "last_id"=>$item["last_id"],
                        "origin_id"=>$l_id
                    ]], $data);
                    $status = 1;
                }else{
                    $uid = $item["user_id"];
                    $user = \app\common\model\User::exists($uid);
                    $data[$uid] = [
                        "user_id" => $uid,
                        "nick_name" => $user["nick_name"],
                        "status" => 0,
                        "last_id" => $l_id,
                        "origin_id" => $item["last_id"],
                    ];
                }
            }
        }
        header("content-type:application/json");
        $this->ajaxSuccess('', ["msg_data" => $data, "msg_status"=>$status]);
    }

    /*
     * 发送消息
     */
    public function send_msg()
    {
        $uid = input("user_id");
        $content = input("con");
        $type = input("type")?input("type"):1;
        if(empty($content)){
            $this->ajaxError("内容不能为空");
        }
        $res = db("message")->insertGetId([
            "send_id"=>0,
            "receive_id"=>$uid,
            "content"=>$content,
            "create_time"=>time(),
            "type"=>$type
        ]);
        header("content-type:application/json");
        if($res){
            $this->ajaxSuccess("发送成功", $res);
        }else{
            $this->ajaxError("发送失败");
        }
    }
    /*
     * 发送消息
     */
    public function send_img()
    {
        $uid = input("user_id");
        $type = 2;
        $storage = new Storage();
        $res = $storage->upload("file", ["png", "jpg", "jpeg"]);
        if($res["status"] == 0){
            //上传成功
            $content = $res["data"];
        }else{
            //上传失败
            $this->ajaxError("发送失败");
        }
        $res = db("message")->insertGetId([
            "send_id"=>0,
            "receive_id"=>$uid,
            "content"=>$content,
            "create_time"=>time(),
            "type"=>$type
        ]);
        header("content-type:application/json");
        if($res){
            $this->ajaxSuccess("发送成功", $content);
        }else{
            $this->ajaxError("发送失败");
        }
    }

    /**
     * 获取聊天记录
     */
    public function getChatList()
    {
        $last_id  = input("last_id");
        $user_id = input("user_id");
        $list = db("message")->where("id", ">", $last_id)->where("send_id", $user_id)->where("receive_id", 0)->select();
        $l_id = db("message")->where("id", ">", $last_id)->where("send_id", $user_id)->order("id desc")->value("id");
        header("content-type:application/json");
        $this->ajaxSuccess('', ['last_id'=>$l_id, 'msg_data'=>$list]);
    }

}